<%@ page import="db.DBConnection" %>
<%@ page import="db.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="db.Account" %>
<%@ page import="db.HistoryUnit" %>
<%
    int user_id = -1;
    String secret1 = "";
    Cookie[] cookies = request.getCookies();

    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("user_id")) {
            user_id = Integer.parseInt(cookie.getValue());
        } else if (cookie.getName().equals("secret")) {
            secret1 = cookie.getValue();
        }
    }
    DBConnection dbConnection = new DBConnection();
    if (!dbConnection.isCookieOk(user_id, secret1)) {
        String redirectURL = "index.jsp";
        response.sendRedirect(redirectURL);
        return;
    }
    int num = dbConnection.doGetNumberOfUnreadMessages(user_id);
%>

<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 07.05.12
  Time: 19:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Accounts</title>
    <link rel="stylesheet" type="text/css" href="style"/>
    <%
        String account_from = request.getParameter("account_from");
        String account_to = request.getParameter("account_to");
        String amount = request.getParameter("amount");
        double  money = 0;
        if (amount != null) {
            money = Double.parseDouble(amount);
        }
        String note = request.getParameter("note");
        boolean isOk = false;
        boolean hasParams = false;
        if (account_from != null && account_to != null && amount != null) {
            hasParams = true;
            isOk = dbConnection.doTransaction(user_id, account_from, account_to, money, note);
        }
    %>
</head>
<body>
<div class="logout" style="text-align:right;"><a href="logout.jsp">  Logout</a></div>
<div class="header"><img src="logo.jpg"></div>
<div class="layout">
    <div class="sidebar">
        <ul class="cssmenu">
            <li><a href="personal_info.jsp">Personal info</a></li>
            <li><a href="accounts.jsp">Accounts</a></li>
            <li><b><a href="operations.jsp">Operations</a></b></li>
            <li><a href="messages.jsp">Messages<% if (num != 0) {%> <b> (<%=num%>)</b> <%}%></a></li>
        </ul>
    </div>
        <%
            ArrayList<Account> accounts = dbConnection.getAccounts(user_id,user_id, DBConnection.OwnerType.CLIENT);
        %>
    <div class="content">
         
            <% if (hasParams) {
                if (isOk) {
                    %> <p style="color: green;"> Transaction completed! </p> <%
                } else{
                    %> <p style="color: red;"> Transaction failed! </p><%
                }
            } %>
        <form method="post" action="transaction.jsp">
            <table class="table">
                <tr>
                    <td width="200"> Account from: <font color="red">*</font></td></td>
                    <td width="200" align="center"> <select name="account_from">
                        <% for (Account account : accounts) {%>
                        <option value=<%=account.number+" ("+account.money+"руб.)"%>> <%=account.number+" ("+account.money+"руб.)"%> </option>
                        <% } %>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td> Account to: <font color="red">*</font></td></td>
                    <td align="center"> <input type="text" name="account_to" size="20"></td>
                </tr>
                <tr>
                    <td> Amount: <font color="red">*</font></td></td>
                    <td align="center"> <input type="text" name="amount" size="20"></td>
                </tr>
                <tr>
                    <td> Note: <font color="red">*</font></td></td>
                    <td align="center"> <input type="text" name="note" size="20"></td>
                </tr>
            </table>
            <input type="submit" value="Do transaction">
        </form>
    </div>
</div>
</body>
</html>